package com.polylink.sys.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.polylink.base.Page;
import com.polylink.sys.bean.Role;
import com.polylink.sys.dao.IFunctionDao;
import com.polylink.sys.dao.IRoleDao;
import com.polylink.sys.service.IRoleService;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:03:48
 *
 **/
@Service
public class RoleService implements IRoleService{
	@Autowired
	private IRoleDao roleDao;
	@Autowired
	private IFunctionDao funDao;

	@Override
	public boolean save(Role role) {
		int i = 0;
		if(role.getId()!=null){
			i = roleDao.update(role);
		}else{
			i = roleDao.add(role);
		}
		if(i>0){
			return true;
		}
		return false;
	}

	@Override
	public Page list(Page p) {
		List<Role> list = roleDao.list(p);
		int count = roleDao.count(p);
		p.setRows(list);
		p.setTotal(count);
		return p;
	}

	@Override
	public boolean delete(String id) {
		int i = roleDao.delete(id);
		if(i>0){
			return true;
		}
		return false;
	}

	@Override
	public List<Map<String, Object>> funList(String roleId) {
		List<Map<String, Object>> list = funDao.getFunTree(roleId);
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		formatterData(data,list,roleId);
		return data;
	}

	/**
	 * 将数据格式化tree数据
	 * @param data
	 * @param list
	 * @param roleId 
	 */
	private void formatterData(List<Map<String, Object>> data,
			List<Map<String, Object>> list, String roleId) {
		//存储已经添加为节点的模块ID，用于排除
		Set<Object> mSet = new HashSet<Object>();
		formatterToTree(mSet,data,list,roleId);
	}
	
	private void formatterToTree(Set<Object> mSet,List<Map<String, Object>> data,
			List<Map<String, Object>> list, String roleId){
		if(list!=null&&list.size()>0){
			for(Map<String,Object> m :list){
				if(m.containsKey("parent_id")&&m.get("parent_id")!=null){
					//模块是否被记录
					if(!mSet.contains(m.get("mode_id"))){
						//添加模块节点
						addTreeNode(data, m.get("mname"), m.get("mode_id"), m.get("parent_id"),false,true);
						mSet.add(m.get("mode_id"));
					}
					//如果和当前角色ID相同则勾选
					//添加功能
					if(m.get("id")!=null){
						addTreeNode(data, m.get("fname"), m.get("id")+"_"+m.get("mode_id"),m.get("mode_id"),
								(m.get("role_id")!=null&&roleId.equals(m.get("role_id").toString())),false);
					}
				}else{
					//父模块为空为顶级节点
					addTreeNode(data, m.get("mname"), m.get("mode_id"), 0,false,true);
					mSet.add(m.get("mode_id"));
				}
			}
		}
	}
	
	/**
	 * 添加树节点
	 * @param data 树节点集合
	 * @param name 节点名
	 * @param id 节点ID
	 * @param pId 节点父ID
	 * @param checked 是否勾选
	 * @param isParent 
	 */
	private void addTreeNode(List<Map<String, Object>> data,Object name,Object id,Object pId, boolean checked, boolean isParent){
		Map<String,Object> map = new  HashMap<String, Object>();
		map.put("text", name);
		map.put("id", id);
		map.put("pId", pId);
		if(checked){
			map.put("checked", true);
		}
		map.put("isParent", isParent);
		data.add(map);
	}

	@Override
	public void saveRoleFun(String roleId, String funId) {
		Set<String> funIds = new HashSet<String>(Arrays.asList(funId.split(",")));
		List<Map<String,Object>> rf = roleDao.getRoleFun(roleId);
		//要删除的关联
		List<String> removeRf = new ArrayList<String>();
		if(rf!=null&&rf.size()>0){
			for(Map<String,Object> map:rf){
				String fId = map.get("function_id").toString();
				if(funIds.contains(fId)){
					//移除不需要更新的，剩下为需要添加的
					funIds.remove(fId);
				}else{
					//需要删除的
					removeRf.add(fId);
				}
			}
		}
		if(funIds!=null&&funIds.size()>0){
			for(String id:funIds){
				if(StringUtils.isNotBlank(id)){
					roleDao.addRoleFunction(roleId,id);
				}
			}
		}
		if(removeRf!=null&&removeRf.size()>0){
			for(String id:removeRf){
				if(StringUtils.isNotBlank(id)){
					roleDao.delRoleFunction(roleId,id);
				}
			}
		}
	}

}
